// pages/login/login.js
let util = require("../../utils/util");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    password: "",
    openId: ""
  },

  idInput: function (e) {
    const that = this
    // console.log(e.detail)
    that.id = e.detail.value
  },

  passwordInput: function (e) {
    const that = this
    that.password = e.detail.value
  },

  toVeri: function (e) {
    const that = this
    if (util.isNull(that.id)) {
      wx.showModal({
        title: '提示',
        content: '请先输入学号',
        showCancel: false
      })
    }
    else if (util.isNull(that.password)) {
      wx.showModal({
        title: '提示',
        content: '请先输入密码',
        showCancel: false
      })
    }
    else {
      var info =
      {
        userId: that.id,
        password: that.password,
      }
      wx.cloud.callFunction({
        name: 'doPost',
        data: {
          url: app.globalData.url + '/user/userCheck',
          info: info
        },
      }).then(res => {
        console.log(res.result)
        if (res.result != null) {
          if (res.result.openId == "") {
            wx.showModal({
              title: '请确认与该账号绑定',
              content: '学号:' + res.result.userId + '\n' + '\n姓名:' + res.result.name,
              success: function (res) {
                if (res.cancel) {
                  //点击取消,默认隐藏弹框
                } else {
                  //点击确定
                  that.lockWechat()
                }
              },
            })
          }
          else {
            wx.showModal({
              title: '提示',
              content: '该用户已绑定微信',
              showCancel: false
            })
          }
        }
        else {
          wx.showModal({
            title: '提示',
            content: '学号或密码不正确',
            showCancel: false
          })
        }
      })
    }
  },

  lockWechat: function (e) {
    const that = this
    var info =
    {
      userId: that.id,
      password: that.password,
      openId: that.openId
    }
    wx.cloud.callFunction({
      name: 'doPost',
      data: {
        url: app.globalData.url + '/user/lockWechat',
        info: info
      },
    }).then(res => {
      console.log(res.result)
      if(res.result==true){
        wx.showModal({
          title: "提示",
          content: "绑定成功!",
          showCancel: false
        })
      }
      else{
        wx.showModal({
          title: "提示",
          content: "绑定失败，该微信已绑定另一个用户",
          showCancel: false
        })
      }
      
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const that = this
    wx.cloud.callFunction({
      name: 'getOpenId',
    }).then(res => {
      that.openId = res.result.openid
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})